﻿using SC;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;

public class SCServer {

    public SC.SocketServer socketServer;
    public delegate void SCRecvEvent(Socket socketSend, SC.MessageType messageType, byte[] objBuf);
    public SCRecvEvent scRecvEvent;

    public SCServer(string ip,int port)
    {
        socketServer = new SC.SocketServer(ip,port)
        {
            clientConnectedEvent = ClientConnected,
            clientForceDisconnect = ClientDisconnect,
            recvEvent = SCReceiveEvent
        };
        socketServer.connectError += ((e) =>
        {
            Console.WriteLine(e);
        });
    }

    private void ClientDisconnect(string ipkey)
    {
        if (clientip.Contains(ipkey))
        {
            clientip.Remove(ipkey);
        }
    }

    private void OnDispose()
    {
        socketServer.Close();
    }

    /// <summary>
    /// 发送对象
    /// </summary>
    /// <param name="messageType"></param>
    /// <param name="obj"></param>
    public void Send(SC.MessageType messageType,object obj)
    {
        if(clientip!=null && clientip.Count > 0 && socketServer!=null)
        {
            for(int i = 0; i < clientip.Count; i++)
            {
                byte[] buffer = SC.MessagePack.SerializeObject(messageType, obj);
                socketServer.Send(clientip[i], buffer);
            }
        }
    }

    /// <summary>
    /// 发送字节流
    /// </summary>
    /// <param name="messageType"></param>
    /// <param name="stream"></param>
    public void SendStream( SC.MessageType messageType, byte[] stream)
    {
        if (clientip != null && clientip.Count > 0 && socketServer != null)
        {
            byte[] buffer = SC.MessagePack.SerializeByteStream(messageType, stream);
            socketServer.SendStream(clientip[0], buffer);
        }
    }

    public void SCReceiveEvent(Socket socket)
    {
        byte[] msgType, objBuf;
        SC.MessagePack.DeserializeBuf(socket, out msgType, out objBuf);
        if (objBuf != null && objBuf.Length > 0)
        {
            MessageType messageType = (MessageType)BitConverter.ToInt32(msgType, 0);
            if (scRecvEvent != null)
            {
                scRecvEvent(socket, messageType, objBuf);
            }
        }
    }

    List<string> clientip = new List<string>(); 
    /// <summary>
    /// 响应客户端接入事件
    /// </summary>
    /// <param name="strIP"></param>
    private void ClientConnected(string strIP)
    {
        if (!clientip.Contains(strIP))
        {
            clientip.Add(strIP);
            //Debug.Log("发送客户端标题配置");
            //this.Send(SC.MessageType.TitleConfig, ControlContentConfig.GetClientTitleConfig());
        }
    }

    public Dictionary<string, bool> heartBeatDic = new Dictionary<string, bool>();
    /// <summary>
    /// 接收到心跳改变该主机状态
    /// </summary>
    /// <param name="ipkey"></param>
    private void BeatHeart(string ipkey)
    {
        if (heartBeatDic.ContainsKey(ipkey))
        {
            heartBeatDic[ipkey] = true;
        }
        else
        {
            heartBeatDic.Add(ipkey, true);
        }
    }

}
